﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using LosConn.BLL;

namespace LosConn.Web.Handler
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : IHttpHandler, IReadOnlySessionState
    {
        private BLL.Los_User bll = new BLL.Los_User();
        public void ProcessRequest(HttpContext context)
        {
            String strResult = String.Empty;
            switch (context.Request["fun"])
            {

                case "f_0001": //新增用户
                    strResult = AddUser(context);
                    break;
                case "f_0002": //修改用户
                    strResult = UpdateUser(context);
                    break;
                case "f_0003": //假删除用户
                    strResult = UpdateUserStatus(context);
                    break;
                case "f_0004": //修改密码
                    strResult = UpdateUserPassWord(context);
                    break;
                default:
                    break;
            }

            context.Response.Write(strResult);
            context.Response.End();
        }

        #region 私有方法
        /// <summary>
        /// 新增用户
        /// </summary>
        private string AddUser(HttpContext context)
        {
            Model.Los_User model = JsonConvert.DeserializeObject<Model.Los_User>(context.Request["data"]);
            model.User_EMail = "";
            if (model.User_UserType.Equals(1))
            {
                model.User_RetailID = 0;
            }
            if (bll.Add(model) > 0)
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        private string UpdateUser(HttpContext context)
        {
            Int32 User_ID = Convert.ToInt32(context.Request.Form["id"]);
            Model.Los_User modelUser = bll.GetModel(User_ID);
            Model.Los_User model = JsonConvert.DeserializeObject<Model.Los_User>(context.Request["data"]);
            model.User_EMail = modelUser.User_EMail;
            model.User_ID = User_ID;
            model.User_PassWord = modelUser.User_PassWord;
            model.User_Name = modelUser.User_Name;
            model.User_RetailID = modelUser.User_RetailID;
            model.User_UserType = modelUser.User_UserType;
            model.User_AddDate = modelUser.User_AddDate;
            if (bll.Update(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        private string UpdateUserPassWord(HttpContext context)
        {
            Model.Los_User model = JsonConvert.DeserializeObject<Model.Los_User>(context.Request["data"]);
            model.User_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.UpdatePassWordNew(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 假删除用户
        /// </summary>
        private string UpdateUserStatus(HttpContext context)
        {
            Model.Los_User model = new Model.Los_User();
            model.User_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.UpdateStatus(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}